package com.feib.soeasy.dao.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.feib.soeasy.dao.CodeTableDao;
import com.feib.soeasy.model.CodeTable;
import com.feib.soeasy.model.Payment;


/**
 * @title (#)CodeTableHibernateDao.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/15 create by Jimmy Liu<br>
 */
@Repository("codeTableDao")
public class CodeTableHibernateDao extends BaseHibernateDao<CodeTable, Long> implements CodeTableDao {

    /* (non-Javadoc)
     * @see com.feib.soeasy.dao.CodeTableDao#findByCodeKind(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<CodeTable> findByCodeKind(String codeKind) {
        if (StringUtils.isEmpty(codeKind))
            return null;
        
        CodeTable codeTable = new CodeTable();
        codeTable.setCodeKind(codeKind);
        
        DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);

        Example example = Example.create(codeTable);
        example.ignoreCase();
        example.enableLike(MatchMode.EXACT);
        example.excludeZeroes();
        criteria.add(example);
        criteria.addOrder(Order.asc("sortIndex"));
        
        return this.getHibernateTemplate().findByCriteria(criteria);        
    }

	@Override
	public CodeTable getCodeTable(String codeKind, String codeValue) {
		Criteria criteria = getSession().createCriteria(CodeTable.class);
		criteria.add(Restrictions.eq("codeKind",codeKind));
		criteria.add(Restrictions.eq("codeValue",codeValue));
		return (CodeTable)criteria.uniqueResult();
	}

}
